<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2018/6/21
 * Time: 19:38
 */

namespace app\index\controller;
use think\Controller;
use think\Exception;
use think\Session;
use app\index\model\User;

class LoginController extends Controller
{
    public function login($returnURL='/'){
        $this->assign('URL',$returnURL);
        return $this->fetch();
    }
    public function register($returnURL='/'){
        $this->assign('URL',$returnURL);
        return $this->fetch();
    }

    public function sign(){
        $captcha = input('vcode');
        $tel=input('tel');
        $pwd=input('pwd');

        $ret=['vcode'=>false,
            'tel'=>false];

        if (captcha_check($captcha)) {
            $ret['vcode']=true;
            $u=User::get(['tel' => $tel]);
            if($u==null){
                $ret['tel']=true;
                $uu=new User();
                $uu->tel=$tel;
                $uu->pwd=$pwd;
                $uu->sex=input('sex');
                $uu->mobile=input('mobile');
                $uu->touxiang='http://cdn.fun100.top/tou1-touxiang';
                $uu->money=0;


                $bb=str_replace(array('年','月','日'),'-',input('birth'));
                if($bb!='')
                    $uu->birth=$bb;

                if($uu->save()){
                    $ret['tel']=true;
                    session('user',$uu->tel);
                    session('user_id',$uu->id);

                }else{
                    sleep(2);
                }
            }
        }
        return json($ret);
    }

    function console_log($data)
    {
        if (is_array($data) || is_object($data))
        {
            echo("<script>console.log('".json_encode($data)."');</script>");
        }
        else
        {
            echo("<script>console.log('".$data."');</script>");
        }
    }


    public function check()
    {
        $captcha = input('vcode');
        $tel=input('tel');
        $pwd=input('pwd');

        $ret=['vcode'=>true,
                'tel'=>false,
                'pwd'=>false];

//        if (captcha_check($captcha)) {
        if(true){
            $ret['vcode']=true;
            $u=User::get(['tel' => $tel]);
            if($u!=null){
                $ret['tel']=true;
                if($u->pwd==$pwd){
                    $ret['pwd']=true;
                    session('user',$u->tel);
                    session('user_id',$u->id);


                }
            }

        }
        return json($ret);

    }
}